import db_utils
import os
import sys

sys.path.append("..")
import settings
from util import string_utils

# 模板参数配置
var_map = {
    "table_entity_name": None,
    "table_name": None,
    "table_columns": None,
    "table_column_spaces": None,
    "table_entity_columns": None,
    "table_entity_columns_all": None,
    "table_entity_column_list": None,
    "primary_key": None,

}

# 声明模板文件路径 和文件保存路径
template_dao = os.path.join(settings.path, "db", "template_dao.txt")
template_entity = os.path.join(settings.path, "db", "template_entity.txt")
dao_path = os.path.join(settings.path, "model", "dao_utils")
entity_path = os.path.join(settings.path, "model", "entity")


def init_var_map(table):
    conn = db_utils.get_conn()
    cur = conn.cursor()

    # 判断数据表是否存在
    cur.execute("SHOW TABLES LIKE '%s'" % table)
    if cur.fetchone() is None:
        print("表名输入错误")
        return False
    var_map["table_entity_name"] = string_utils.underline_to_hump(table)
    var_map["table_name"] = table
    table_columns = ""
    table_column_spaces = ""
    table_entity_columns = ""
    table_entity_columns_all = ""
    table_entity_column_list = ""
    # 获取数据表的详细信息 根据信息 构件代码生成配置
    cur.execute("desc %s" % table)
    column_list = cur.fetchall()
    cur.close()
    conn.close()

    # 构件主键配置与其他字段配置
    for column_info in column_list:
        column_name = column_info[0]
        if 'PRI' in column_info:
            var_map["primary_key"] = column_name
            table_entity_column_list = table_entity_column_list + column_name + "=None\n    "
        else:
            table_columns = table_columns + column_name + ","
            # if 'int' in column_info:
            #     table_column_spaces = table_column_spaces + "%d,"
            # else:
            #     table_column_spaces = table_column_spaces + "'%s',"
            # 这里本来是进行类型判断 生成不同占位符的 但是在批量查询中不知道为啥会报类型错误 所以暂时都改成%s
            table_column_spaces = table_column_spaces + "%s,"
            table_entity_columns = table_entity_columns + "table_entity." + column_name + ","
            table_entity_columns_all = table_entity_columns_all + "entitys." + column_name + ","
            table_entity_column_list = table_entity_column_list + column_name + " = None\n    "
    var_map["table_columns"] = table_columns[:-1]
    var_map["table_column_spaces"] = table_column_spaces[:-1]
    var_map["table_entity_columns"] = table_entity_columns[:-1]
    var_map["table_entity_columns_all"] = table_entity_columns_all[:-1]
    var_map["table_entity_column_list"] = table_entity_column_list

    return True


def code_generate():
    # 创建需要生成代码的文件
    dao_file = open(os.path.join(dao_path, var_map["table_name"] + "_dao.py"), "w", encoding="UTF-8")
    entity_file = open(os.path.join(entity_path, var_map["table_name"] + ".py"), "w", encoding="UTF-8")
    t_dao_file = open(template_dao, "r", encoding="UTF-8")
    t_entity_file = open(template_entity, "r", encoding="UTF-8")
    t_dao = t_dao_file.read()
    t_entity = t_entity_file.read()
    t_dao_file.close()
    t_entity_file.close()

    # 根据参数替换模板中的占位符
    t_dao = replace_mark_dao(t_dao)
    t_entity = replace_mark_entity(t_entity)

    # 写入文件
    dao_file.write(t_dao)
    entity_file.write(t_entity)
    dao_file.close()
    t_entity_file.close()
    print("生成成功~")


def replace_mark_dao(file):
    file = file.replace("#{table_entity_name}", var_map["table_entity_name"])
    file = file.replace("#{table_name}", var_map["table_name"])
    file = file.replace("#{table_columns}", var_map["table_columns"])
    file = file.replace("#{table_column_spaces}", var_map["table_column_spaces"])
    file = file.replace("#{table_entity_columns}", var_map["table_entity_columns"])
    file = file.replace("#{table_entity_columns_all}", var_map["table_entity_columns_all"])
    file = file.replace("#{primary_key}", var_map["primary_key"])
    return file


def replace_mark_entity(file):
    file = file.replace("#{table_entity_name}", var_map["table_entity_name"])
    file = file.replace("#{table_entity_column_list}", var_map["table_entity_column_list"])
    return file


def generate():
    print("请输入表名：")
    table_name = input()
    if not init_var_map(table_name):
        return generate()
    code_generate()


if __name__ == "__main__":
    generate()
